/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.util.List;

import com.jeesite.modules.st.entity.StInventoryBill;
import com.jeesite.modules.st.entity.StWarehouseInventory;
import com.jeesite.modules.st.utils.BillUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StTakeStockBill;
import com.jeesite.modules.st.dao.StTakeStockBillDao;
import com.jeesite.modules.st.entity.StTakeStockDetail;
import com.jeesite.modules.st.dao.StTakeStockDetailDao;

import javax.annotation.Resource;

/**
 * st_take_stock_billService
 *
 * @author HOHD
 * @version 2021-03-09
 */
@Service
@Transactional(readOnly = true)
public class StTakeStockBillService extends CrudService<StTakeStockBillDao, StTakeStockBill> {

    @Autowired
    private StTakeStockDetailDao stTakeStockDetailDao;

    /**
     * 获取单条数据
     *
     * @param stTakeStockBill
     * @return
     */
    @Override
    public StTakeStockBill get(StTakeStockBill stTakeStockBill) {
        StTakeStockBill entity = super.get(stTakeStockBill);
        if (entity != null) {
            StTakeStockDetail stTakeStockDetail = new StTakeStockDetail(entity);
            stTakeStockDetail.setStatus(StTakeStockDetail.STATUS_NORMAL);
            entity.setStTakeStockDetailList(stTakeStockDetailDao.findList(stTakeStockDetail));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param stTakeStockBill 查询条件
     * @return
     */
    @Override
    public Page<StTakeStockBill> findPage(StTakeStockBill stTakeStockBill) {
        return super.findPage(stTakeStockBill);
    }

    @Resource
    BillUtil billUtil;

    /**
     * 保存数据（插入或更新）
     *
     * @param stTakeStockBill
     */
    @Override
    @Transactional(readOnly = false)
    public void save(StTakeStockBill stTakeStockBill) {
        if (stTakeStockBill.getIsNewRecord()) {
            String billNo = billUtil.billNo(BillUtil.BillType.takeStockBill);
            stTakeStockBill.setBillNo(billNo);
        }
        super.save(stTakeStockBill);
        // 保存 StTakeStockBill子表
        for (StTakeStockDetail stTakeStockDetail : stTakeStockBill.getStTakeStockDetailList()) {
            if (!StTakeStockDetail.STATUS_DELETE.equals(stTakeStockDetail.getStatus())) {
                stTakeStockDetail.setParentId(stTakeStockBill);
                if (stTakeStockDetail.getIsNewRecord()) {
                    stTakeStockDetailDao.insert(stTakeStockDetail);
                } else {
                    stTakeStockDetailDao.update(stTakeStockDetail);
                }
            } else {
                stTakeStockDetailDao.delete(stTakeStockDetail);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param stTakeStockBill
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(StTakeStockBill stTakeStockBill) {
        super.updateStatus(stTakeStockBill);
    }

    @Autowired
    @Lazy
    StInventoryBillService stInventoryBillService;

    /**
     * 删除数据
     *
     * @param stTakeStockBill
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(StTakeStockBill stTakeStockBill) {
        super.delete(stTakeStockBill);
        StTakeStockDetail stTakeStockDetail = new StTakeStockDetail();
        stTakeStockDetail.setParentId(stTakeStockBill);
        List<StTakeStockDetail> stTakeStockDetailList = stTakeStockBill.getStTakeStockDetailList();
        String id = stTakeStockBill.getId();
        StInventoryBill query = new StInventoryBill();
        query.setPrimaryId(id);
        List<StInventoryBill> list = stInventoryBillService.findList(query);
        for (StInventoryBill stInventoryBill : list) {
            stInventoryBillService.delete(stInventoryBill);
        }
        stTakeStockDetailDao.deleteByEntity(stTakeStockDetail);
    }

}